﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Billing.Utilities;
using Billing.Dtos;
using System.Windows.Controls.Primitives;
using Billing.Controls;

namespace Billing.Windows
{
    public partial class CancelBill : Window
    {
        public CancelBill()
        {
            InitializeComponent();
            txtBillNo.Focus();
            lblText.Text = "Fill BillNo & Reason and Press Enter to Cancel";
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.Close();
            }
            else if (e.Key == Key.Enter)
            {
                if (txtBillNo.Text != string.Empty && txtReason.Text != string.Empty)
                {
                    if (Database.CancelBill(txtBillNo.Text, txtReason.Text) > 0)
                    {
                        lblText.Text = "Bill Cancelled";
                        txtBillNo.Text = string.Empty;
                        txtReason.Text = string.Empty;
                    }
                    else
                    {
                        lblText.Text = "Invalid Bill";
                        txtBillNo.Text = string.Empty;
                        txtReason.Text = string.Empty;
                    }
                    return;
                }
                else if (txtBillNo.IsFocused)
                {
                    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
                    UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
                    if (elementWithFocus != null)
                        elementWithFocus.MoveFocus(request);
                }
            }
        }
    }
}